package com.hyyt.shopjsp.phoneApp.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.phoneApp.pojo.PhoneAppHomeCategoryInfo;
import com.hyyt.shopjsp.phoneApp.service.IPhoneAppHomeCategoryInfoService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * phone_app_home_category_info, PHONE_手机APP_端分类广告信息 Controller
 *
 */

@Controller
@RequestMapping("/back/phoneAppHomeCategoryInfo")
public class PhoneAppHomeCategoryInfoController extends BaseController {

    private Logger log = Logger.getLogger(PhoneAppHomeCategoryInfoController.class);
    private String basePath = "/back/phoneApp/phoneAppCategoryInfo/";

    @Resource
    private IPhoneAppHomeCategoryInfoService phoneAppHomeCategoryInfoService;

    /**
     * 进入手机首页分类信息维护
     * @param param
     * @return
     */
    @RequestMapping("gotoPhoneAppHomeCategoryInfoPage")
    public String gotoPhoneAppHomeCategoryInfoPage(Map<String, Object> param){
        param.put("categoryId", "1");//手机首页分类id
        return basePath+"categoryInfoList";
    }

    /**
     * 查询所有手机首页分类信息维护
     * @param categoryId   手机首页分类id
     * @param title     主题
     * @param response
     * @param isShow    是否显示
     */
    @RequestMapping(value="listPhoneAppHomeCategoryInfo",method= RequestMethod.POST)
    @ResponseBody
    public void listPhoneAppHomeCategoryInfo(String categoryId,String title,String isShow, Page page, HttpServletResponse response){
        Map<String, Object> param = new HashMap<>();
        param.put("categoryId", categoryId);
        if(Utils.objectIsNotEmpty(title)){
            param.put("title", title.trim());
        }
        if(Utils.objectIsNotEmpty(isShow)){
            param.put("isShow", isShow);
        }
        param.put("sortColumns", "sortCode asc");
        int totalRecordCount =phoneAppHomeCategoryInfoService.countPhoneAppHomeCategoryInfo(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<PhoneAppHomeCategoryInfo> list =phoneAppHomeCategoryInfoService.queryPhoneAppHomeCategoryInfo(param, page.getPageRecordBeginIndex(), page.getPageSize());
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", list);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 根据手机首页分类信息维护id获得对象
     *
     * @param categoryInfoId
     *
     */
    @RequestMapping(value = "getPhoneAppHomeCategoryInfoObj", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getPhoneAppHomeCategoryInfoObj(Integer categoryInfoId) {
        if (Utils.objectIsNotEmpty(categoryInfoId)) {
            PhoneAppHomeCategoryInfo phoneAppHomeCategoryInfo = phoneAppHomeCategoryInfoService.loadPhoneAppHomeCategoryInfo(categoryInfoId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(phoneAppHomeCategoryInfo));
        }
        return ResultMsg.create(false);
    }

    /**
     * 保存或者修改手机首页分类信息维护
     *
     * @param phoneAppHomeCategoryInfo
     * @return
     */
    @RequestMapping(value = "saveOrUpdatePhoneAppHomeCategoryInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdatePhoneAppHomeCategoryInfo(PhoneAppHomeCategoryInfo phoneAppHomeCategoryInfo) {
        if (Utils.objectIsNotEmpty(phoneAppHomeCategoryInfo)) {
            BasicUsers basicUsers = getSessionUsers();
            if (Utils.objectIsEmpty(phoneAppHomeCategoryInfo.getCategoryInfoId())) {
                Timestamp createTime = new Timestamp(System.currentTimeMillis());
                phoneAppHomeCategoryInfo.setCreateTime(createTime);
                phoneAppHomeCategoryInfo.setPublishUser(basicUsers.getUserName());
                phoneAppHomeCategoryInfoService.savePhoneAppHomeCategoryInfo(phoneAppHomeCategoryInfo);
            } else {
                Timestamp updateTime = new Timestamp(System.currentTimeMillis());
                phoneAppHomeCategoryInfo.setUpdateTime(updateTime);
                phoneAppHomeCategoryInfo.setModifyUser(basicUsers.getUserName());
                phoneAppHomeCategoryInfoService.updatePhoneAppHomeCategoryInfo(phoneAppHomeCategoryInfo);
            }
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     * 根据传入的手机首页分类信息维护id删除
     *
     * @param categoryInfoId
     */
    @RequestMapping(value = "deleteCategoryInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteCategoryInfo(Integer categoryInfoId) {
        if (Utils.objectIsNotEmpty(categoryInfoId)) {
            phoneAppHomeCategoryInfoService.deletePhoneAppHomeCategoryInfo(categoryInfoId);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }
    }

}
